import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:flutter_ext/home/ext/constant.dart' as ext;
import 'package:flutter_ext/home/page/post_page.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parse;

class ArticlePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new ArticleState();
}

class ArticleState extends State<ArticlePage>{
  List<Post> mPostList;


  @override
  void initState() {
    super.initState();
    mPostList = new List();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: const Text('Article'),),
      body: new FutureBuilder(
          future: getPostHtml(),
          builder: (BuildContext context,AsyncSnapshot snapshot){
            switch (snapshot.connectionState){
              case ConnectionState.waiting:
              case ConnectionState.none:
                return  ext.buildLoadingIndicator();
              default:
                if(snapshot.hasError){
                  return ext.buildExceptionIndicator('-error on response-');
                }else{
                  var html = snapshot.data;
                  var document = parse.parse(html);
                  List<dom.Element> container = document.getElementsByClassName('u-full-width');
                  List<dom.Element> elements = container[0].getElementsByClassName('title');
                  List<dom.Element> times = container[0].getElementsByClassName('time');
                  mPostList.clear();
                  elements.forEach((element){
                    List<dom.Element> head = element.getElementsByTagName('a');
                    List<dom.Element> author = element.getElementsByTagName('small');
                    mPostList.add(new Post(
                        head[0].text,
                        times[elements.indexOf(element)].text,
                        author[0].text.replaceAll('\(', '').replaceAll('\)', ''),
                        'http://gank.io./'+head[0].attributes['href']));
                  });
                  return _createPostList();
                }
            }
          }),
    );
  }

  Future<String> getPostHtml() async{
    var httpClient = new HttpClient();
    final url = 'http://gank.io/post/published';
    var request = await
    httpClient.getUrl(Uri.parse(url));
    var response = await
    request.close();
    return await
    response.transform(utf8.decoder).join();
  }

  Widget _buildLoadingIndicator(){
    return new Center(
      child: new Card(
        elevation: 4.0,
        child: new Padding(
          padding: const EdgeInsets.all(8.0),
          child: new Wrap(direction: Axis.vertical,
          spacing: 8.0,
          children: <Widget>[
            new CupertinoActivityIndicator(),
            new Text("loading ...")
          ],
          crossAxisAlignment: WrapCrossAlignment.center,
          ),),),
    );
  }

  Widget _createPostList() {
    return new ListView.builder(
        itemCount: mPostList.length,
        itemBuilder: (context,index){
          return _buildPostRow(mPostList[index]);
        });
  }

  Widget _buildPostRow(Post data) {
    return new InkWell(
      onTap: (){
        Navigator.push(context,
          new MaterialPageRoute(
              builder: (BuildContext context){
                return new PostPage(data.toJson());
              }));
      },
      child: new Card(
        margin: new EdgeInsets.all(2.0),
        child: new Padding(
          padding: new EdgeInsets.all(8.0),
          child: new Column(
            children: <Widget>[
              new Container(
                margin: new EdgeInsets.fromLTRB(2.0, 4.0,2.0, 4.0),
                child: new Align(
                  alignment: Alignment.centerLeft,
                  child: new Text(
                    data.desc,
                    style: new TextStyle(
                      fontSize: 16.0,
                      color: Colors.black,
                      fontWeight: FontWeight.bold
                    ),
                  ),
                )
        ),
        new Container(
          margin: const EdgeInsets.fromLTRB(2.0,10.0,10.0,4.0),
        ),
          new Row(children: <Widget>[
            new Align(
              alignment: Alignment.centerLeft,
              child: new Text(data.who,
                  style: new TextStyle(
                      fontSize: 12.0,
                      color: Colors.grey)),
            ),
            new Expanded(
                child: new Align(alignment: Alignment.centerRight,
                child: new Text(data.publishAt),
                )),
          ],
          )
      ],),
    ),
      ),
    );
  }
}

class Post{
  String publishAt;
  String url;
  String who;
  String desc;

  Post(this.publishAt, this.url, this.who, this.desc);

  Map<String,dynamic> toJson()=>{
    'desc': desc,
    'publishAt': publishAt,
    'who': who,
    'url': url
  };

}